package com.example.mytestapplication;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.example.mytestapplication.bean.BoxDeviceVo;
import com.example.mytestapplication.bean.DataValue;
import com.example.mytestapplication.utils.LiefengRetrofit;

import org.simple.eventbus.EventBus;

import java.io.File;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.R.attr.filter;
import static android.R.attr.start;
import static android.R.attr.tag;

/**
 * Created by wei on 2017/8/10.
 */

public class RecordService extends Service {
    private static String TAG = "RecordService";
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            isRunning = true;
            getVoiceForNet();
            handler.postDelayed(this,15*1000);
        }
    };
    private boolean isRunning;
    private Context mContext;

    private void getVoiceForNet() {
        Log.d(TAG, "getVoiceForNet: 获取录音");
        updataWeight();
        EventBus.getDefault().post("");
    }

    private void updataWeight(){
        AppWidgetManager instance = AppWidgetManager.getInstance(mContext);
        RemoteViews views = new RemoteViews(mContext.getPackageName(),R.layout.layout_widget_provider2);
        views.setImageViewResource(R.id.notice_img1,R.drawable.come_back);
        Intent intent = new Intent("action_noice_btn1");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.notice_img1,pendingIntent);
        ComponentName componentName = new ComponentName(mContext,MyWidgetProvider2.class);
        instance.updateAppWidget(componentName,views);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        mContext = getApplicationContext();
        TAG = getClass().getName();
        Log.d(TAG, "onCreate: "+TAG);
        Log.d(TAG, "onCreate: "+getClass().getSimpleName());
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: 启动服务");
        if (isRunning) {
            handler.removeCallbacksAndMessages(null);
            handler.post(runnable);
        }else{
            handler.post(runnable);
            isRunning = true;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: 销毁service");
        //销毁了发广播，重新开启该服务。
        mContext.sendBroadcast(new Intent("restart_service"));
//        StartRecordServiceReceiver startRecordServiceReceiver = new StartRecordServiceReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("");
//        registerReceiver(startRecordServiceReceiver,filter);
    }
}
